package com.jerry.walking.module.activityTravel.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.walking.R;
import com.jerry.walking.base.BaseApplication;
import com.jerry.walking.base.BaseFragment;
import com.jerry.walking.base.BaseViewHolder;
import com.jerry.walking.base.MyBaseAdapter;
import com.jerry.walking.constant.Urls;
import com.jerry.walking.module.activityTravel.activity.FirstActivity;
import com.jerry.walking.module.activityTravel.bean.TravelRecommend1;
import com.jerry.walking.utils.OkHttpUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Yang on 2016/5/10.
 *
 *          推荐 fragment   主界面
 */
public class RecommendFragment extends BaseFragment {

    @BindView(R.id.lv_travel_recommend_1)
    PullToRefreshListView listView;

    List<TravelRecommend1.LogsBean> items;
    private int page = 1;
    private MyBaseAdapter<TravelRecommend1.LogsBean> adapter;

    private ImageLoader imageLoader;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recommend;
    }

    @Override
    protected void initViews() {
        super.initViews();

        imageLoader = BaseApplication.getApplication().getImageLoader();

        items = new ArrayList<>();

        adapter = new MyBaseAdapter<TravelRecommend1.LogsBean>(RecommendFragment.this.getActivity(), items) {
            @Override
            public int getLayoutId() {
                return R.layout.adapter_travel_recommend;
            }

            @Override
            public BaseViewHolder getViewHolder() {
                return new ViewHolder();
            }

            @Override
            public void bindViews(View view, BaseViewHolder viewHolder) {
                ViewHolder holder = (ViewHolder) viewHolder;


                imageLoader.displayImage(getItem(holder.position).getCover(), holder.imageView1);
                imageLoader.displayImage(getItem(holder.position).getCreated_by().getAvatar(), holder.imageView2);

                holder.textView1.setText(getItem(holder.position).getName());
                holder.textView2.setText(getItem(holder.position).getView_count()+"");
                holder.textView3.setText(getItem(holder.position).getFav_count()+"");
                holder.textView4.setText(getItem(holder.position).getComment_count());

                //时间日期
//                String created_date = getItem(holder.position).getCreated_date();
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
//                String format = simpleDateFormat.format(new Date(Long.parseLong(created_date)));

                String created_date = getItem(holder.position).getStart_date();

                holder.textView5.setText(
                        created_date+"·"+getItem(holder.position).getTotal_days()+"天"+getItem(holder.position).getPhoto_number()+"图·"+"by"+getItem(holder.position).getCreated_by().getNickname()
                );

            }
        };
        listView.setAdapter(adapter);

        //设置listview的刷新模式
        listView.setMode(PullToRefreshBase.Mode.BOTH);

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                items.clear();
                page = 1;
                listView.setMode(PullToRefreshBase.Mode.BOTH);
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                loadData();
            }
        });

        // listview 点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(RecommendFragment.this.getActivity(), FirstActivity.class);
                intent.putExtra("id", items.get(position - 1).getId());
                RecommendFragment.this.getActivity().startActivity(intent);
            }
        });
    }

    @Override
    protected void loadData() {
        super.loadData();

        OkHttpUtils.get(Urls.RECOMMEND_LIST+page+Urls.RECOMMEND_LIST_END, new OkHttpUtils.OnGetDataListener() {
            @Override
            public void onResponse(String url, String data) {
                Gson gson = new Gson();

                TravelRecommend1 travelRecommend1 = gson.fromJson(data, TravelRecommend1.class);

                List<TravelRecommend1.LogsBean> logs = travelRecommend1.getLogs();

                if (travelRecommend1.getCur_page() == travelRecommend1.getTotal_pages()) {
                    listView.onRefreshComplete();
                    listView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);//修改模式只允许下拉刷新
                }

                items.addAll(logs);
                adapter.notifyDataSetChanged();
                listView.onRefreshComplete();
            }

            @Override
            public void onFailure(String url, String error) {

            }
        });

    }

    /**
     * 该类提供listview适配器中的所有资源id
     */
    class ViewHolder extends BaseViewHolder {

        @BindView(R.id.iv_travel_recommend_adapter)
        ImageView imageView1;
        @BindView(R.id.iv_travel_recommend_adapter4)
        ImageView imageView2;

        @BindView(R.id.tv_travel_recommend_adapter)
        TextView textView1;

        @BindView(R.id.tv_travel_recommend_adapter1)
        TextView textView2;

        @BindView(R.id.tv_travel_recommend_adapter2)
        TextView textView3;

        @BindView(R.id.tv_travel_recommend_adapter3)
        TextView textView4;

        @BindView(R.id.tv_travel_recommend_adapter4)
        TextView textView5;
    }
}
